/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cadastros;

import dao.DaoServico;
import java.math.BigInteger;
import java.sql.Time;
import modelo.Servico;
import util.mensagem;

/**
 *
 * @author fumio
 */
public class CadServico extends javax.swing.JDialog {

    /**
     * Creates new form CadServico
     */
    public CadServico(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        edtCodigo = new javax.swing.JTextField();
        edtServico = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        edtDescricao = new javax.swing.JTextField();
        edtTempo = new javax.swing.JTextField();
        edtComissao = new javax.swing.JTextField();
        edtCusto = new javax.swing.JTextField();
        edtPrecoFinal = new javax.swing.JTextField();
        edtPontos = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        btnConfirmar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel4.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        getContentPane().add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 538, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Código:");
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, -1, -1));

        jLabel2.setText("Serviço:");
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, -1));

        jLabel3.setText("Descrição:");
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, -1, -1));

        jLabel4.setText("Tempo Est.:");
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

        edtCodigo.setEnabled(false);
        jPanel2.add(edtCodigo, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 10, 50, -1));
        jPanel2.add(edtServico, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 50, 320, -1));

        jLabel5.setText("Comissão:");
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 120, 70, -1));

        jLabel6.setText("Custo Serviço:");
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, -1, -1));

        jLabel7.setText("Preço Serviço:");
        jPanel2.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 160, -1, -1));

        jLabel8.setText("Pontos:");
        jPanel2.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 200, -1, -1));
        jPanel2.add(edtDescricao, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 80, 320, -1));
        jPanel2.add(edtTempo, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 110, 90, -1));
        jPanel2.add(edtComissao, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 110, 100, -1));
        jPanel2.add(edtCusto, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 150, 90, -1));
        jPanel2.add(edtPrecoFinal, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 150, 100, -1));
        jPanel2.add(edtPontos, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 190, 90, -1));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 460, 250));

        jPanel1.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnConfirmar.setText("Confirmar");
        btnConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmarActionPerformed(evt);
            }
        });
        jPanel1.add(btnConfirmar, new org.netbeans.lib.awtextra.AbsoluteConstraints(14, 29, -1, -1));

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        jPanel1.add(btnCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 20, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 250, 460, 90));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-470)/2, (screenSize.height-368)/2, 470, 368);
    }// </editor-fold>//GEN-END:initComponents

    private void btnConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmarActionPerformed
        // TODO add your handling code here:
        
        if (validar()){
            DaoServico dao = new DaoServico();
            dao.salvar(Long.parseLong(edtCodigo.getText()),
                    edtServico.getText(),
                    edtDescricao.getText(),
                    Time.valueOf(edtTempo.getText()),
                    Double.parseDouble(edtComissao.getText()),
                    Double.parseDouble(edtCusto.getText()),
                    Double.parseDouble(edtPrecoFinal.getText()),
                    Integer.parseInt(edtPontos.getText()));
        }
    }//GEN-LAST:event_btnConfirmarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CadServico dialog = new CadServico(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConfirmar;
    private javax.swing.JTextField edtCodigo;
    private javax.swing.JTextField edtComissao;
    private javax.swing.JTextField edtCusto;
    private javax.swing.JTextField edtDescricao;
    private javax.swing.JTextField edtPontos;
    private javax.swing.JTextField edtPrecoFinal;
    private javax.swing.JTextField edtServico;
    private javax.swing.JTextField edtTempo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    // End of variables declaration//GEN-END:variables
    public boolean validar(){
        if(edtServico.getText().equals("")){
            new mensagem(null,true, "Atenção o Campo Serviço é Obrigatório!").setVisible(true);
            edtServico.requestFocusInWindow();
            return false;
        }else if(edtDescricao.getText().equals("")){
            new mensagem(null, true, "Atenção o Campo Descrição é Obrigatório!").setVisible(true);
            edtDescricao.requestFocusInWindow();
            return false;
        }else{
            if(edtCodigo.getText().equals("")){
                edtCodigo.setText("0");
            }
            if(edtComissao.getText().equals("0")){
                edtComissao.setText("0.00");
            }
            if (edtPrecoFinal.getText().equals("")){
                edtPrecoFinal.setText("0.00");
            }
            if(edtTempo.getText().equals("")){
                edtTempo.setText("00:00:00");
            }
            if(edtCusto.getText().equals("")){
                edtCusto.setText("0");
            }
            if(edtPontos.getText().equals("")){
                edtPontos.setText("0");
            }
            return true;
        }
        
    }
    
    public void Alterar(Servico servico){
        
        edtCodigo.setText(servico.getCod_servico().toString());
        edtServico.setText(servico.getServico().toString());
        edtDescricao.setText(servico.getDescricao().toString());
        System.out.println(servico.getComissao());
        edtComissao.setText(servico.getComissao().toString());
        edtPrecoFinal.setText(servico.getVlr_final().toString());
        edtCusto.setText(servico.getVlr_custo().toString());
        edtPontos.setText(servico.getPontos().toString());
        this.setVisible(true);
        
    }


}
